package cn.stylefeng.guns.utils;

import cn.stylefeng.roses.kernel.model.response.ResponseData;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigDecimal;

public class HttpBaseRequest {
    private HttpServletRequest request;
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getRequest() {
        return request;
    }
    // 字符串读取
    // 方法一
    public static String ReadAsChars(HttpServletRequest request) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try
        {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null)
            {
                sb.append(str);
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != br)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public HttpBaseRequest(HttpServletRequest request){
        this.request = request;
    }

    public String getString(String key){
        String value = "";
        try{
            value = request.getParameter(key);
            if(value == null){
                value = "";
            }
        }catch (Exception e){
            value = "";
        }
        return value;
    }

    public Long getLong(String key){
        Long value = getLong(key,new Long(-1));
        return value;
    }

    public Long getLong(String key,Long dfvalue){
        Long value;
        try{
            value = Long.parseLong(request.getParameter(key));
        }catch (Exception e){
            value = dfvalue;
        }
        return value;
    }

    public Long getLong(String key,long dfvalue){
        Long value;
        try{
            value = Long.parseLong(request.getParameter(key));
        }catch (Exception e){
            value = dfvalue;
        }
        return value;
    }

    public Integer getInt(String key){
        Integer value = getInt(key,new Integer(-1));
        return value;
    }

    public Integer getInt(String key,Integer dfvalue){
        Integer value;
        try{
            value = Integer.parseInt(request.getParameter(key));
        }catch (Exception e){
            value = dfvalue;
        }
        return value;
    }

    public Integer getInt(String key,int dfvalue){
        Integer value;
        try{
            value = Integer.parseInt(request.getParameter(key));
        }catch (Exception e){
            value = dfvalue;
        }
        return value;
    }

    public BigDecimal getDecimal(String key){
        BigDecimal value = getDecimal(key,new BigDecimal(-1));
        return value;
    }

    public BigDecimal getDecimal(String key,BigDecimal dfvalue){
        BigDecimal value;
        try{
            value = new BigDecimal(request.getParameter(key));
        }catch (Exception e){
            value = dfvalue;
        }

        return value;
    }

    public double getDouble(String key,double dfvalue){
        double value = dfvalue;
        try{
            value = Double.parseDouble(request.getParameter(key));
        }catch (Exception e){
            value = dfvalue;
        }

        return value;
    }

    //校验参数是否合格
    private boolean ischecked = true;
    private  String errmsg = "";
    public boolean CheckParam(){
        return this.ischecked;
    }
    public String ErrorMsg(){
        return this.errmsg;
    }

    public ResponseData toError(){
        return ResponseData.error(this.errmsg);
    }
    //检查是否为空的参数
    public HttpBaseRequest checkNullParam(String key, String msg){
        if(!ischecked){
            return this;
        }
        if(request.getParameter(key) == null || request.getParameter(key).equals("")){
            this.ischecked = false;
            this.errmsg = msg;
        }

        return this;
    }

    public <T> HttpBaseRequest checkDefaultParam(String key, String msg, T dfvalue){
        try{
            if(!this.ischecked){
                return this;
            }
            if (request.getParameter(key) == null){
                this.errmsg = msg;
                this.ischecked = false;
                return this;
            }

            T value = (T)request.getParameter(key);
            if(value == dfvalue){
                this.errmsg = msg;
                this.ischecked = false;
            }
        }catch (Exception e){
            this.errmsg = "参数转换失败";
            this.ischecked = false;
        }

        return this;
    }
}
